namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services.Monitoring
{
    /// <summary>
    /// 系统性能监控服务接口
    /// </summary>
    public interface ISystemPerformanceMonitor
    {
        /// <summary>
        /// 获取当前系统性能信息
        /// </summary>
        Task<SystemPerformanceInfo> GetCurrentPerformanceAsync();

        /// <summary>
        /// 获取性能历史记录
        /// </summary>
        Task<IEnumerable<PerformanceMetric>> GetMetricsHistoryAsync(TimeSpan period);

        /// <summary>
        /// 获取性能统计信息
        /// </summary>
        Task<PerformanceStatistics> GetPerformanceStatisticsAsync(TimeSpan period);

        /// <summary>
        /// 检查系统性能警告
        /// </summary>
        Task<IEnumerable<PerformanceAlert>> CheckPerformanceAlertsAsync();

        /// <summary>
        /// 开始监控
        /// </summary>
        Task StartMonitoringAsync();

        /// <summary>
        /// 停止监控
        /// </summary>
        Task StopMonitoringAsync();
    }

    /// <summary>
    /// 系统性能信息
    /// </summary>
    public class SystemPerformanceInfo
    {
        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// CPU使用率 (%)
        /// </summary>
        public float CpuUsagePercent { get; set; }

        /// <summary>
        /// 内存使用率 (%)
        /// </summary>
        public float MemoryUsagePercent { get; set; }

        /// <summary>
        /// 已使用内存 (MB)
        /// </summary>
        public float MemoryUsedMB { get; set; }

        /// <summary>
        /// 总内存 (MB)
        /// </summary>
        public float MemoryTotalMB { get; set; }

        /// <summary>
        /// 磁盘使用率 (%)
        /// </summary>
        public float DiskUsagePercent { get; set; }

        /// <summary>
        /// 已使用磁盘空间 (GB)
        /// </summary>
        public float DiskUsedGB { get; set; }

        /// <summary>
        /// 磁盘总空间 (GB)
        /// </summary>
        public float DiskTotalGB { get; set; }

        /// <summary>
        /// 进程数量
        /// </summary>
        public int ProcessCount { get; set; }

        /// <summary>
        /// 线程数量
        /// </summary>
        public int ThreadCount { get; set; }

        /// <summary>
        /// 句柄数量
        /// </summary>
        public int HandleCount { get; set; }

        /// <summary>
        /// 系统运行时间 (分钟)
        /// </summary>
        public int UptimeMinutes { get; set; }

        /// <summary>
        /// 网络连接数
        /// </summary>
        public int NetworkConnectionCount { get; set; }

        /// <summary>
        /// 性能等级
        /// </summary>
        public PerformanceLevel PerformanceLevel { get; set; }

        /// <summary>
        /// 扩展属性
        /// </summary>
        public Dictionary<string, object> ExtendedProperties { get; set; } = new();
    }

    /// <summary>
    /// 性能指标记录
    /// </summary>
    public class PerformanceMetric
    {
        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// CPU使用率 (%)
        /// </summary>
        public float CpuUsagePercent { get; set; }

        /// <summary>
        /// 内存使用率 (%)
        /// </summary>
        public float MemoryUsagePercent { get; set; }

        /// <summary>
        /// 磁盘使用率 (%)
        /// </summary>
        public float DiskUsagePercent { get; set; }

        /// <summary>
        /// 进程数量
        /// </summary>
        public int ProcessCount { get; set; }

        /// <summary>
        /// 线程数量
        /// </summary>
        public int ThreadCount { get; set; }

        /// <summary>
        /// 句柄数量
        /// </summary>
        public int HandleCount { get; set; }

        /// <summary>
        /// 性能等级
        /// </summary>
        public PerformanceLevel PerformanceLevel { get; set; }
    }

    /// <summary>
    /// 性能统计信息
    /// </summary>
    public class PerformanceStatistics
    {
        /// <summary>
        /// 统计开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 统计结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 样本总数
        /// </summary>
        public int TotalSamples { get; set; }

        /// <summary>
        /// 平均CPU使用率 (%)
        /// </summary>
        public float AverageCpuUsage { get; set; }

        /// <summary>
        /// 最大CPU使用率 (%)
        /// </summary>
        public float MaxCpuUsage { get; set; }

        /// <summary>
        /// 最小CPU使用率 (%)
        /// </summary>
        public float MinCpuUsage { get; set; }

        /// <summary>
        /// 平均内存使用率 (%)
        /// </summary>
        public float AverageMemoryUsage { get; set; }

        /// <summary>
        /// 最大内存使用率 (%)
        /// </summary>
        public float MaxMemoryUsage { get; set; }

        /// <summary>
        /// 最小内存使用率 (%)
        /// </summary>
        public float MinMemoryUsage { get; set; }

        /// <summary>
        /// 平均磁盘使用率 (%)
        /// </summary>
        public float AverageDiskUsage { get; set; }

        /// <summary>
        /// 最大磁盘使用率 (%)
        /// </summary>
        public float MaxDiskUsage { get; set; }

        /// <summary>
        /// 最小磁盘使用率 (%)
        /// </summary>
        public float MinDiskUsage { get; set; }
    }

    /// <summary>
    /// 性能警告
    /// </summary>
    public class PerformanceAlert
    {
        /// <summary>
        /// 警告类型
        /// </summary>
        public AlertType Type { get; set; }

        /// <summary>
        /// 警告级别
        /// </summary>
        public AlertLevel Level { get; set; }

        /// <summary>
        /// 警告消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 当前值
        /// </summary>
        public float Value { get; set; }

        /// <summary>
        /// 阈值
        /// </summary>
        public float Threshold { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 扩展属性
        /// </summary>
        public Dictionary<string, object> ExtendedProperties { get; set; } = new();
    }

    /// <summary>
    /// 性能等级
    /// </summary>
    public enum PerformanceLevel
    {
        /// <summary>
        /// 未知
        /// </summary>
        Unknown = 0,

        /// <summary>
        /// 优秀
        /// </summary>
        Excellent = 1,

        /// <summary>
        /// 良好
        /// </summary>
        Good = 2,

        /// <summary>
        /// 一般
        /// </summary>
        Fair = 3,

        /// <summary>
        /// 较差
        /// </summary>
        Poor = 4,

        /// <summary>
        /// 严重
        /// </summary>
        Critical = 5
    }

    /// <summary>
    /// 警告类型
    /// </summary>
    public enum AlertType
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        CpuUsage = 1,

        /// <summary>
        /// 内存使用率
        /// </summary>
        MemoryUsage = 2,

        /// <summary>
        /// 磁盘使用率
        /// </summary>
        DiskUsage = 3,

        /// <summary>
        /// 网络使用率
        /// </summary>
        NetworkUsage = 4,

        /// <summary>
        /// 进程数量
        /// </summary>
        ProcessCount = 5,

        /// <summary>
        /// 线程数量
        /// </summary>
        ThreadCount = 6
    }

    /// <summary>
    /// 警告级别
    /// </summary>
    public enum AlertLevel
    {
        /// <summary>
        /// 信息
        /// </summary>
        Info = 1,

        /// <summary>
        /// 警告
        /// </summary>
        Warning = 2,

        /// <summary>
        /// 严重
        /// </summary>
        Critical = 3
    }

    /// <summary>
    /// 系统性能监控配置选项
    /// </summary>
    public class SystemPerformanceOptions
    {
        public const string SectionName = "SystemPerformance";

        /// <summary>
        /// 是否启用监控
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 是否自动启动
        /// </summary>
        public bool AutoStart { get; set; } = true;

        /// <summary>
        /// 数据采集间隔（秒）
        /// </summary>
        public int CollectionIntervalSeconds { get; set; } = 30;

        /// <summary>
        /// 数据保留天数
        /// </summary>
        public int RetentionDays { get; set; } = 7;

        /// <summary>
        /// 内存中最大指标数量
        /// </summary>
        public int MaxMetricsInMemory { get; set; } = 2880; // 24小时 * 60分钟 * 2 (30秒间隔)

        /// <summary>
        /// 是否启用警告
        /// </summary>
        public bool EnableAlerts { get; set; } = true;

        /// <summary>
        /// CPU使用率警告阈值 (%)
        /// </summary>
        public float CpuWarningThreshold { get; set; } = 80.0f;

        /// <summary>
        /// CPU使用率严重阈值 (%)
        /// </summary>
        public float CpuCriticalThreshold { get; set; } = 95.0f;

        /// <summary>
        /// 内存使用率警告阈值 (%)
        /// </summary>
        public float MemoryWarningThreshold { get; set; } = 80.0f;

        /// <summary>
        /// 内存使用率严重阈值 (%)
        /// </summary>
        public float MemoryCriticalThreshold { get; set; } = 95.0f;

        /// <summary>
        /// 磁盘使用率警告阈值 (%)
        /// </summary>
        public float DiskWarningThreshold { get; set; } = 80.0f;

        /// <summary>
        /// 磁盘使用率严重阈值 (%)
        /// </summary>
        public float DiskCriticalThreshold { get; set; } = 95.0f;

        /// <summary>
        /// 是否启用详细日志
        /// </summary>
        public bool EnableDetailedLogging { get; set; } = false;
    }
}
